package org.swidgets.sinputdata;

import java.util.Date;

import org.swidgets.base.sinputbox.SDateInputBox;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;



public class SInputData  extends HorizontalPanel implements FocusListener
{
	final PopupPanel 		popuppanel = new PopupPanel(true);
	final CalendarWidget	cp = new CalendarWidget();
	//final Button 			inputButton = new Button("data");
	final SDateInputBox		inputField = new SDateInputBox();
	final Image				imgCal=new Image();

	private ChangeListenerCollection changeListeners;
	private String data = "";

	public SInputData() {
		super();
		inputField.addFocusListener(this);
		createCalendarField();
		cp.dataOdierna();
	}
	
	public SInputData(Date data){
		this();
		cp.setDate(data);
		fromCpToInputField();
	}
	
	public SInputData(int year,int month,int day){
		this();
		cp.setDate(year, month, day);
		fromCpToInputField();
	}

	public SInputData(String label){
		this();
		inputField.setLabel(label);
	}
	
	public SInputData(String label, int year,int month,int day){
		this(label);
		cp.setDate(year, month, day);
		fromCpToInputField();
	}
	
	public SInputData(String label,Date data){
		this(label);
		cp.setDate(data);
		fromCpToInputField();
	}
	
	
	public void clear() {
		inputField.setText("");
		cp.dataOdierna();
	}

	public void setToday() {
		cp.dataOdierna();
		fromCpToInputField();
	}

	public void setDate(Date data) {
		if (data!=null) {
			cp.modificaDataDate(data);
			fromCpToInputField();
		}
	}

	/*
	 *	Torna una data corretta o null
	 */
	public Date getDate() {
		String data = getTextDate();
		if (data.length()>0) {
			return Utilita.stringToDate(data);
		}

		return null;
	}

	/*
	 * Torna o Stringa nulla o una data in formato valido
	 */
	public String getTextDate() {
//		if (!FieldValidator.txtEmpty(inputField)&&!FieldValidator.txtInvalidData(inputField)) {
//			return inputField.getText().trim();
//		}
		if(inputField.isValidated())
			return inputField.getText().trim();
		return "";
	}

	public void setFldStyleName(String style){
		inputField.setStyleName(style);
	}

	public void setEnabled(boolean enabled) {
		inputField.setEnabled(enabled);
		//inputButton.setEnabled(enabled);
	}

	public boolean isEnabled() {
		return inputField.isEnabled();
	}

	public boolean isEmpty() {
		return inputField.getText()== null || inputField.getText().trim().length()==0;
	}

	private void createCalendarField() {
		cp.addChangeListener(new ChangeListener() {
    	  public void onChange(Widget sender) {
    		  	fromCpToInputField();
				popuppanel.hide();
    	  }
    	});
    	cp.addPopupListener(new PopupListener() {
			public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
				popuppanel.hide();
			}
    	});
		popuppanel.add(cp);

		imgCal.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (!inputField.isEnabled()) {
					return;
				}
	        	//if(!FieldValidator.txtEmpty(inputField)&&!FieldValidator.txtInvalidData(inputField)) {
				if(inputField.isValidated()){
	        		fromInputFieldToCp();
	        	}
	        	else {
	        		cp.dataOdierna();
	        	}

	            int left = sender.getAbsoluteLeft() + 5;
	            int top = sender.getAbsoluteTop() + 5;
	            popuppanel.setPopupPosition(left, top);
	        	popuppanel.show();
	        }
	    });
		//imgCal.setStyleName("imgCal");
		imgCal.setUrl("style/img/calendario.gif");
		//inputButton.setText("Calendario");
		//inputButton.setStyleName(sBtnCalendario);

		setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		setSpacing(2);

		add(inputField);
		add(imgCal);
	}

	private void fromCpToInputField() {
		try{
			data = inputField.getText();
			inputField.setText(Utilita.formattaData(cp.getDate()));
			if(changeListeners != null && !changeListeners.isEmpty() && !data.equalsIgnoreCase(inputField.getText())) {
				changeListeners.fireChange(this);
			}
		}catch(Exception e){}
	}

	private void fromInputFieldToCp(){
		try {
			String tmp = getTextDate();
			if (tmp.length()>0) {
				cp.modificaDataDate(Utilita.stringToDate(tmp));
			}
		}
		catch(Exception e){}
	}

	public SDateInputBox getInputField() {
		return inputField;
	}

	public void onFocus(Widget sender) {
		// TODO Auto-generated method stub
		data = inputField.getText();
	}

	public void onLostFocus(Widget sender) {
		// TODO Auto-generated method stub
		if(changeListeners != null && !changeListeners.isEmpty() && !data.equalsIgnoreCase(inputField.getText())) {
			changeListeners.fireChange(this);
		}
	}

	public void addChangeListener(ChangeListener listener) {
	    if (changeListeners == null) {
	    	changeListeners = new ChangeListenerCollection();
	    }
	    changeListeners.add(listener);
	}

	public void removeChangeListener(ChangeListener listener) {
	    if (changeListeners != null) {
	    	changeListeners.remove(listener);
	    }
	}
	
	public void setLabel(String label){
		inputField.setLabel(label);
	}
}